package com.example.rpc.server;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitServer {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = RabbitConfig.PUBLISH_QUEUE)
    public void reply(Message message){
        System.out.println("服务端收到消息:"+message.getBody().toString());

        Message response = MessageBuilder.withBody(("i'm receive并作出响应:"+new String(message.getBody())).getBytes()).build();
        CorrelationData correlationData = new CorrelationData(message.getMessageProperties().getCorrelationId());
        rabbitTemplate.sendAndReceive(RabbitConfig.EXCHANGE_NAME, RabbitConfig.REPLY_QUEUE, response, correlationData);
    }

}
